#!/usr/bin/env bash
#
# Summary: Configures JAVA_HOME for GUI applications.
#
# Usage: jenv macos-javahome
#
# Installs a file located at ~/Library/LaunchAgents/jenv-environment.plist . It sets JAVA_HOME for GUI
# applications on startup for the **currently active version of Java**.
#
# This **must be updated** whenever the version of Java changes. It does not update dynamically.
#
#
cat << EOT > ${HOME}/Library/LaunchAgents/jenv-environment.plist
<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
   <key>Label</key>
   <string>my.startup</string>
   <key>ProgramArguments</key>
   <array>
     <string>sh</string>
     <string>-c</string>
     <string>
     launchctl setenv JAVA_HOME $(jenv javahome) ;
     </string>
   </array>
   <key>RunAtLoad</key>
   <true/>
 </dict>
 </plist>
EOT
launchctl unload ${HOME}/Library/LaunchAgents/jenv-environment.plist || true
launchctl load ${HOME}/Library/LaunchAgents/jenv-environment.plist
